<?php

namespace App\Admin\Controllers\User;

use Illuminate\Http\Request;
use App\Admin\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    // 登录页面
    public function index()
    {
        return view('admin.login.index');
    }

    // 登录提交
    public function login(Request $request)
    {
        // 验证
        $rules = [
            'name' => 'required|min:2',
            'password' => 'required|min:5|max:16',
        ];
        $this->validate($request, $rules);

        // 保存
        $user = $request->only(['name', 'password']);
        if (Auth::guard('admin')->attempt($user)) {
            return redirect('/admin/home');
        }

        // 渲染
        return back()->withErrors(['用户名密码不正确']);
    }

    // 登出行为
    public function logout()
    {
        Auth::guard('admin')->logout();
        return redirect('/admin/user/login');
    }
}
